home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / rlogin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  4.1 KB  |  180 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "socket.h"
  5. #include "session.h"
  6. #include "proc.h"
  7. #include "tty.h"
  8. #include "commands.h"
  9. #include "netuser.h"
  10.  
  11. static char *username = "guest";
  12. static char terminal[] = "ansi";
  13. static char *termspeed = "/38400";
  14.  
  15. int rlo_connect __ARGS((struct session *sp,char *fsocket,int len));
  16. void rlo_output __ARGS((int unused,void *p1,void *p2));
  17. void rlrecv __ARGS((struct session *sp));
  18.  
  19. /* Execute user rlogin command */
  20. int
  21. dorlogin(argc,argv,p)
  22. int argc;
  23. char *argv[];
  24. void *p;
  25. {
  26.     struct session *sp;
  27.     struct sockaddr_in fsocket;
  28.     struct sockaddr_in lsocket;
  29.  
  30.     /* Allocate a session descriptor */
  31.     if((sp = newsession(argv[1],RLOGIN)) == NULLSESSION){
  32.         tprintf("Too many sessions\n");
  33.         return 1;
  34.     }
  35.     fsocket.sin_family = AF_INET;
  36.     if(argc < 3)
  37.         fsocket.sin_port = IPPORT_RLOGIN;
  38.     else
  39.         fsocket.sin_port = atoi(argv[2]);
  40.    
  41.    tprintf("Resolving %s... ",sp->name);
  42.     if((fsocket.sin_addr.s_addr = resolve(sp->name)) == 0L){
  43.         tprintf(Badhost,sp->name);
  44.         keywait(NULLCHAR,1);
  45.         freesession(sp);
  46.         return 1;
  47.     }
  48.     if((sp->s = socket(AF_INET,SOCK_STREAM,0)) == -1){
  49.         tprintf("Can't create socket\n");
  50.         keywait(NULLCHAR,1);
  51.         freesession(sp);
  52.         return 1;
  53.     }
  54.     lsocket.sin_family = AF_INET;
  55.     lsocket.sin_addr.s_addr = INADDR_ANY;
  56.     lsocket.sin_port = IPPORT_RLOGIN;
  57.     bind(sp->s,(char *)&lsocket,sizeof(lsocket));
  58.     return rlo_connect(sp,(char *)&fsocket,SOCKSIZE);
  59. }
  60. /* Generic interactive connect routine */
  61. int
  62. rlo_connect(sp,fsocket,len)
  63. struct session *sp;
  64. char *fsocket;
  65. int len;
  66. {
  67.     unsigned int index;
  68.  
  69.     index = sp - Sessions;
  70.  
  71.     tprintf("Trying %s...\n",psocket((struct sockaddr *)fsocket));
  72.     if(connect(sp->s,fsocket,len) == -1){
  73.           tprintf("%s session %u failed: %s errno %d\n",
  74.          Sestypes[sp->type], index, sockerr(sp->s),errno);
  75.  
  76.         keywait(NULLCHAR,1);
  77.         freesession(sp);
  78.         return 1;
  79.     }
  80.     tprintf("%s session ",Sestypes[sp->type]);
  81.     tprintf("%u connected to %s\n",index,sp->name);
  82.     rlrecv(sp);
  83.     return 0;
  84. }
  85.  
  86. /* Rlogin input routine, common to both rlogin and ttylink */
  87. void
  88. rlrecv(sp)
  89. struct session *sp;
  90. {
  91.     int c,s,index;
  92.     char *cp;
  93.  
  94.     s = sp->s;
  95.  
  96.     /* We run both the network and local sockets in transparent mode
  97.      * because we have to do our own eol mapping
  98.      */
  99.     seteol(s,"");
  100.     seteol(Curproc->input,"");
  101.     seteol(Curproc->output,"");
  102.  
  103.     /* Read real keystrokes from the keyboard */
  104.     sp->ttystate.crnl = 0;
  105.     /* Put tty into raw mode */
  106.     sp->ttystate.echo = 0;
  107.     sp->ttystate.edit = 0;
  108.  
  109.     setflush(s,'\n');
  110.  
  111.     index = sp - Sessions;
  112.  
  113.     /* Fork off the transmit process */
  114.     sp->proc1 = newproc("rlo_out",1024,rlo_output,0,sp,NULL);
  115.    
  116.    /* Process input on the connection */
  117.     while((c = recvchar(s)) != -1){
  118.         tputc((char)c);
  119.     } 
  120.     
  121. quit:    /* A close was received from the remote host.
  122.      * Notify the user, kill the output task and wait for a response
  123.      * from the user before freeing the session.
  124.      */
  125.     cp = sockerr(s);
  126.     seteol(s,"\r\n");
  127.     seteol(Curproc->input,"\r\n");
  128.     seteol(Curproc->output,"\r\n");
  129.  
  130.     tprintf("%s session %u", Sestypes[sp->type],index);
  131.     tprintf(" closed: %s\n", cp != NULLCHAR ? cp : "EOF");
  132.     killproc(sp->proc1);
  133.     sp->proc1 = NULLPROC;
  134.     close_s(sp->s);
  135.     sp->s = -1;
  136.     keywait(NULLCHAR,1);
  137.     freesession(sp);
  138. }
  139.  
  140. /* User rlogin output task, started by user rlogin command */
  141. void
  142. rlo_output(unused,sp1,p)
  143. int unused;
  144. void *sp1;
  145. void *p;
  146. {
  147.     struct session *sp;
  148.     struct mbuf *bp;
  149.     char *cp;
  150.     char *logname;
  151.     sp = (struct session *)sp1;
  152.  
  153.     logname = getenv("USER");
  154.     if(logname == NULLCHAR)
  155.         logname = username;
  156.     bp = ambufw(1 + strlen(logname)+1 + strlen(logname)+1 +
  157.         strlen(terminal) + strlen(termspeed)+1);
  158.  
  159.     cp = bp->data;
  160.     *cp++ = '\0';
  161.     strcpy(cp, logname);
  162.     cp += strlen(logname) + 1;
  163.     strcpy(cp, logname);
  164.     cp += strlen(logname) + 1;
  165.     strcpy(cp, terminal);
  166.     cp += strlen(terminal);
  167.     strcpy(cp, termspeed);
  168.     cp += strlen(termspeed) + 1;
  169.     bp->cnt = cp - bp->data;
  170.     if(send_mbuf(sp->s,bp,0,NULLCHAR,0) != -1){
  171.         /* Send whatever's typed on the terminal */
  172.         while(recv_mbuf(sp->input,&bp,0,NULLCHAR,0) > 0){
  173.             if(send_mbuf(sp->s,bp,0,NULLCHAR,0) == -1)
  174.             break;
  175.         }
  176.     }
  177.     /* Make sure our parent doesn't try to kill us after we exit */
  178.     sp->proc1 = NULLPROC;
  179. }
  180.